/*
 * @Description: file content
 */
const NewsService = require("../../services/admin/NewsService")

const NewsController = {
  add: async (req, res) => {
    const { title, content, category, isPublish, } = req.body
    const coverPicture = req.file ? `/newsuploads/${req.file.filename}` : ''
    await NewsService.add({ title, content, editTime: new Date(), isPublish: Number(isPublish), coverPicture, category: Number(category), })
    res.send({
      code: 200,
      msg: '添加成功',
    })
  },
  getList: async (req, res) => {
    const id = req.params.id
    const result = await NewsService.getList(id)
    res.send({
      code: 200,
      data: result,
      msg: '数据获取成功',
    })
  },
  delOne: async (req, res) => {
    const id = req.params.id
    await NewsService.delOne({ _id: id })
    res.send({
      code: 200,
      msg: '删除成功',
    })
  },
  updateOne: async (req, res) => {
    const { title, content, category, isPublish, _id } = req.body
    const coverPicture = req.file ? `/newsuploads/${req.file.filename}` : ''

    await NewsService.updateOne({ _id, title, content, editTime: new Date(), isPublish: Number(isPublish), coverPicture, category: Number(category), })
    res.send({
      code: 200,
      msg: '更新成功',
    })
  },
  publish: async (req, res) => {
    await NewsService.publish({ ...req.body, editTime: new Date(), })
    res.send({
      code: 200,
      msg: '更新成功',
    })
  },
}
module.exports = NewsController
